package utils

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

var Status = struct {
	Ok          int
	Error       int
	ServerError int
	NotFound    int
	AuthError   int
}{
	Ok:          200,
	Error:       502,
	ServerError: 500,
	NotFound:    404,
	AuthError:   501,
}

type BaseResponse struct {
	Msg  string      `json:"msg"`
	Code int         `json:"code"`
	Data interface{} `json:"data"`
}

func ShouldBind(c *gin.Context, body interface{}) (err error) {
	if err = c.ShouldBind(&body); err != nil {
		validatorErr, _ := err.(validator.ValidationErrors)
		Response(
			c,
			err.Error(),
			Status.Error,
			RemoveTopStruct(validatorErr.Translate(Trans)),
		)
		return validatorErr
	}
	return err
}

func Response(c *gin.Context, msg string, code int, data interface{}) {
	c.JSON(
		200,
		BaseResponse{
			Msg:  msg,
			Code: code,
			Data: data,
		},
	)
}
